var api = require('../../../../api.js');
var utils = require('../../../../utils.js');
// var WxParse = require('../../../../wxParse/wxParse.js');

var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        last_time: '',
        is_show: true,


        yinsi: true,
        shiyong: true
    },
    // 获取手机号
    have_tel: function(e) {
        console.log(e.detail.value);
        let that = this;
        that.setData({
            tel_num: e.detail.value
        })
    },
    // 查看填写验证码
    have_yzm: function(e) {
        console.log(e.detail.value);
        let that = this;
        that.setData({
            yzm_num: e.detail.value
        })

    },


    //   验证码
    clickVerify: function() {
        var that = this;
        // 将获取验证码按钮隐藏60s，60s后再次显示
        that.setData({
            is_show: !that.data.is_show //false
        })
        settime(that);

        console.log("yam")
        app.request({
            url: api.casa.sms_code,
            data: {
                code: 123456,
                tel: that.data.tel_num
            },
            success: function(res) {
                console.log(res)
                that.setData({
                    user_yzm: res.data.yzm
                })
            }
        })
    },
    // 注册
    sign_in: function(t) {
        console.log(t.detail.value)
        let that = this;
        
        //    接口返回的验证码
        let yzm = that.data.user_yzm;
        //      用户输入的验证码
        let int_yzm = that.data.yzm_num;
        console.log(yzm, int_yzm)
        if (yzm == int_yzm) {
            console.log("yzm ==")
            app.request({
                url: api.casa.binding_tel,
                data: {
                    tel: that.data.tel_num
                },
                success: function (res) {
                    if (res.code == 2) {
                        console.log(res.code)
                        wx.showModal({
                            title: '对不起',
                            content: '此号码已经注册',
                            showCancel: true,
                        })
                        return false;
                    }
                    if (res.code == 1) {
                        console.log(res.code)
                        wx.showModal({
                            title: '对不起',
                            content: '注册失败',
                            showCancel: true,
                        })
                        return false;
                    }
                    if(res.code == 0){
                        console.log(res.code)
                        wx.showToast({
                            title: '注册成功',
                            icon: 'success',
                            duration: 1500,
                            mask: true,
                            success: function (res) {
                                wx.redirectTo({
                                    url: "/page/casa/pages/myself/myself",
                                })
                            },
                        })
                    }

                    
                },
            })
        }
    },
    // 隐私政策
    yinsi: function() {
        let that = this;
        that.setData({
            yinsi: !that.data.yinsi
        })
    },
    // 使用条款
    shiyong: function() {
        let that = this;
        that.setData({
            shiyong: !that.data.shiyong
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.loadData()
    },
    // 跳转 获取用户信息
    loadData: function (e) {
        var a = this;
        a.setData({
            store: wx.getStorageSync("store")
        });
        var t = wx.getStorageSync("pages_user_user");
        t && a.setData(t), app.request({
            url: api.user.index,
            success: function (e) {
                console.log(e);
                0 == e.code && (a.setData(e.data), wx.setStorageSync("pages_user_user", e.data),
                    wx.setStorageSync("share_setting", e.data.share_setting), wx.setStorageSync("user_info", e.data.user_info),
                    console.log(e.data));
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})


var countdown = 60;
var settime = function(that) {
    if (countdown == 0) {
        that.setData({
            is_show: true
        })
        countdown = 60;
        return;
    } else {
        that.setData({
            is_show: false,
            last_time: countdown
        })

        countdown--;
    }
    setTimeout(function() {
        settime(that)
    }, 1000)
}